package com.jt.redis.demos;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class CarService {
    @Autowired
    private RedisTemplate redisTemplate;
    //向购物车添加数据
    public void addCart(Integer userId,String productId,int num){
        HashOperations hashOperations
                = redisTemplate.opsForHash();
        //key不存在则会自动创建
        hashOperations.increment("cart:"+userId, productId, num);
    }
    public Map<String,Object> listCart(Integer userId){
        HashOperations hashOperations
                = redisTemplate.opsForHash();
        return hashOperations.entries("cart:"+userId);

    }
}